home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group97a.txt / 000119_icon-group-sender _Sat Apr 19 08:08:45 1997.msg < prev    next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Received: from kingfisher.CS.Arizona.EDU by cheltenham.cs.arizona.edu; Mon, 21 Apr 1997 09:27:46 MST
  2. Received: by kingfisher.CS.Arizona.EDU; (5.65v3.2/1.1.8.2/08Nov94-0446PM)
  3.     id AA04801; Mon, 21 Apr 1997 09:27:45 -0700
  4. Message-Id: <3358C3DD.23EE@airmail.net>
  5. Date: Sat, 19 Apr 1997 08:08:45 -0500
  6. From: Nathanael Graham <nathang@airmail.net>
  7. X-Mailer: Mozilla 2.01 (Win95; U)
  8. Mime-Version: 1.0
  9. To: icon-group@cs.arizona.edu
  10. Subject: Comments on Icon Routine
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. Errors-To: icon-group-errors@cs.arizona.edu
  14. Status: RO
  15. Content-Length: 2775
  16.  
  17. To fellow Iconers:
  18.  
  19.    This week at work I wanted to take documents created by MS-Word or 
  20. WordPerfect and break down the 80+ character lines into lines of no more 
  21. than 80 characters and I figured that this would be a perfect use for 
  22. Icon.  The routine did not take too long to code and, at the suggestion 
  23. of a fellow worker, I generalized it to allow for lines of any specified 
  24. length.  Assuming the Icon routine is called truncate.icn/truncate.exe,
  25. one would type the following on the command line:
  26.  
  27.     truncate infile.txt outfile.txt 80
  28.  
  29. This would:
  30.  
  31.     . Read in each record from infile.txt
  32.     . After deleting leading spaces, it would write the record to       
  33.       outfile.txt, if the record length was 80 or less
  34.     . If the record length was greater than 80, then it would "break up" 
  35.       the record on spaces, scanning for them from position 81 and going 
  36.       backwards.  Once it found the space, it would write from the      
  37.       beginning of the record to the space and repeat the same          
  38.       algorithm starting with what was left of the record after the     
  39.       space.  If no space was found in any record or piece thereof of   
  40.       length 80 or more, then the first 80 characters would be written, 
  41.       and the rest of the record would be handled per above.
  42.  
  43.    I've been writing Icon utilities for a number of years off and on, 
  44. but still consider myself somewhat of a beginner.  I am posting this 
  45. routine to the Icon list in the hopes that more experienced 
  46. practicioners of the language will point out ways in which I can more 
  47. fully utilize the unique strengths of Icon.
  48.  
  49.    Thanks for your help in advance.
  50.  
  51. Steve Graham
  52.  
  53. ========================================================================
  54. procedure main(files)
  55.    in      := open(files[1],"r") | stop("Problem opening " || files[1])
  56.    out     := open(files[2],"w") | stop("Problem opening " || files[2])
  57.    rmargin := files[3]
  58.    while line := read(in) do {
  59.       if *(line := ltrim(line)) < (rmargin + 1)
  60.          then write(out,line)
  61.          else {
  62.                while *(line := ltrim(line)) > rmargin do {
  63.                   if line[rmargin+1] == " "
  64.                      then {
  65.                            write(out,line[1:rmargin + 1])
  66.                            line[1:rmargin + 1] := ""
  67.                           }
  68.                      else {
  69.                            i := upto(" ",reverse(line[1:rmargin + 1])) | 
  70. 0
  71.                            write(out,line[1:(rmargin + 1)-i])
  72.                            line[1:(rmargin + 1)-i] := ""
  73.                  }        }
  74.                if *line > 0
  75.                   then write(out,line)
  76.      }        }
  77.    close(out)
  78.    close(in)
  79. end
  80.  
  81. procedure ltrim(txt)
  82.    while any(' ',txt) do txt[1] := ""
  83.    return txt
  84. end
  85.  
  86.